//
//  DYAgreementVC.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/11.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYAgreementVC.h"
#import <WebKit/WebKit.h>

@interface DYAgreementVC ()<WKNavigationDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)WKWebView *webView;
@property (nonatomic,strong)UILabel *titleLabel;

@end

@implementation DYAgreementVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)dy_initUI
{
    [super dy_initUI];
    

    if ([self.navigationItem.title isEqualToString:@"注册协议"]) {
        [self createLeftBarItem:@"nav_back" titleString:nil];
    }

    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.titleLabel];
    [self.scrollView addSubview:self.webView];
    
}
-(void)leftButtonClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    //修改字体颜色  #9098b8
    //    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#8A8A8A'"completionHandler:nil];
    [webView evaluateJavaScript:@"document.body.scrollHeight"
              completionHandler:^(id result, NSError *_Nullable error) {
                  CGFloat height = [result floatValue];
                  _webView.frame = CGRectMake(0,self.titleLabel.xhq_bottom+BILIHEIGHT(10),kScreenWidth, height);
                  self.scrollView.contentSize=CGSizeMake(0, self.webView.xhq_bottom);
                  
              }];
    XHQHUDHIDE(self.view);
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    XHQHUDHIDE(self.view);
}
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel=[Utils labelWithTitleFontSize:18 textColor:[UIColor xhq_aTitle] alignment:1];
        _titleLabel.frame=CGRectMake(BILIWIDTH(60), BILIHEIGHT(10), kScreenWidth-BILIWIDTH(120), BILIHEIGHT(20));
        _titleLabel.lineBreakMode=NSLineBreakByTruncatingTail;
        _titleLabel.text=@"卡福智能管家注册协议";
    }
    return _titleLabel;
}

- (WKWebView *)webView
{
    if (!_webView)
    {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
        WKUserContentController *userContent = [[WKUserContentController alloc]init];
        configuration.userContentController = userContent;
        //web 自适应屏幕宽度js
        NSString *jsString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';};";
        WKUserScript *script = [[WKUserScript alloc]initWithSource:jsString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [userContent addUserScript:script];
        
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0,self.titleLabel.xhq_bottom+BILIHEIGHT(10), kScreenWidth,0) configuration:configuration];
        _webView.navigationDelegate = self;
    }
    return _webView;
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavigationStatusHeight)];
        _scrollView.alwaysBounceVertical=YES;
    }
    return _scrollView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
